package 哨兵;

public class Node1 {
    Node head=new Node(666,null);
    static class Node {
        int value;
        Node next;

        public Node(int value, Node next) {
            this.next = next;
            this.value = value;
        }
    }
    public void loop1(){
        Node p=head.next;
        while (p!=null){
            System.out.println(p.value);
            p=p.next;
        }
    }
    public void addFirst(int value){
        head=new Node(value,head);//头指针不存储数据
    }
       public Node findnode(int index){
        int i=0;
        for(Node p = head; p!=null; p=p.next,i++){
            if(i==index){
                return p;
            }
        }
        return null;
    }
    public Node findLast(){//p.next的不为null时才可以
        if (head==null){
            return null;
        }
        Node p;
        for(p=head;p.next!=null;p=p.next){
        }
        return p;
    }
    public void addLast(int value){
        Node last=findLast();
        if(last==null){
            addFirst(value);
        }
        last.next=new Node(value,null);
    }
    public void removeFirst(){
        if(head==null){
            throw new IllegalArgumentException(
                    String.format("不合法")
            );
        }
        head=head.next;
    }
   /* public void remove(int index){
        if(index==0){
            removeFirst();
            return;
        }
        Node pre=findnode(index-1);
        if(pre==null){
            throw new IllegalArgumentException(
                    String.format("不合法")
            );
        }
        Node remove=pre.next;
        if(remove==null)
        */
    }

